package git.soulbgm.common.resolver;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.util.AntPathMatcher;
import org.apache.shiro.util.PatternMatcher;

/**
 * @author SoulBGM
 * @version 1.0
 * @date 2019/10/14 14:08
 */
public class UrlPermission implements Permission {

    private String url;

    public UrlPermission(String url) {
        this.url = url;
    }

    @Override
    public boolean implies(Permission p) {
        if (!(p instanceof UrlPermission)) {
            return false;
        }
        UrlPermission urlPermission = (UrlPermission) p;

        PatternMatcher patternMatcher = new AntPathMatcher();

        // this.url(来自数据库中存放的通配符数据),在 Realm 的授权方法中注入的
        // urlPermission.url(来自浏览器正在访问的链接)

        return patternMatcher.matches(this.url, urlPermission.url);
    }
}
